package com.lemeng.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSONObject;
import com.github.wxpay.sdk.MyWxPayUtil;
import com.github.wxpay.sdk.WXPayUtil;
import com.lemeng.dao.OrderItemMapper;
import com.lemeng.dao.OrderLogMapper;
import com.lemeng.dao.OrderMapper;
import com.lemeng.pojo.Order;
import com.lemeng.pojo.OrderItem;
import com.lemeng.pojo.OrderLog;
import com.lemeng.service.OrderService;
import com.lemeng.util.IdWorker;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderService.class)
public class OrderServiceImpl implements OrderService {

    @Autowired
    OrderItemMapper orderItemMapper;
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    IdWorker idWorker;
    @Autowired
    OrderLogMapper orderLogMapper;
    @Autowired
    RabbitTemplate rabbitTemplate;


    @Override
    public String towxPay(String ordersn) {

        Order order = orderMapper.selectByPrimaryKey(ordersn);

        //把商品名称查出来
        OrderItem orderItem = new OrderItem();
        orderItem.setOrderId(ordersn);
        List<OrderItem> orderItemList = orderItemMapper.select(orderItem);


        return MyWxPayUtil.toPay(orderItemList.get(0).getName(),ordersn,order.getPayMoney());
    }

    @Override
    @Transactional
    public void wxPaySuccess(String str) {

        //1验证参数
        //result_code 和 return_code 都必须是success验证签名
        //2修改订单状态
        //3 通知前台

        try {
            Map<String, String> map = WXPayUtil.xmlToMap(str);

            if ("SUCCESS".equals(map.get("result_code"))&&"SUCCESS".equals(map.get("return_code"))) {

                //验证签名
                if (MyWxPayUtil.isSign(map)) {
                    System.out.println("签名验证成功");

                    String ordersn = map.get("out_trade_no");
                    String transaction_id = map.get("transaction_id");
                    Order order = orderMapper.selectByPrimaryKey(ordersn);

                    if (order==null||"1".equals(order.getPayStatus())) {
                        return;
                    }

                    order.setPayStatus("1");//已支付
                    order.setOrderStatus("1");//待发货
                    order.setUpdateTime(new Date());
                    order.setPayTime(order.getUpdateTime());
                    order.setTransactionId(transaction_id);

                    orderMapper.updateByPrimaryKeySelective(order);


                    //订单日志
                    OrderLog orderLog = new OrderLog();
                    orderLog.setId(idWorker.nextId()+"");
                    orderLog.setRemarks("用户支付成功");
                    orderLog.setOperater("SYSTEM");
                    orderLog.setOperateTime(new Date());
                    orderLog.setOrderId(ordersn);
                    orderLog.setOrderStatus("1");
                    orderLog.setPayStatus("1");

                    orderLogMapper.insert(orderLog);

                    System.out.println("订单状态修改成功");
                    //通知前台

                    HashMap hashMap = new HashMap();
                    hashMap.put("ordersn",ordersn);
                    hashMap.put("paymoney",order.getPayMoney());
                    rabbitTemplate.convertAndSend("paynotify","", JSONObject.toJSONString(hashMap));
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
